﻿using FileBrowser.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace FileBrowser
{
    public sealed partial class FolderItems : UserControl
    {
        
        public FolderItems()
        {
            this.InitializeComponent();
            this.DefaultStyleKey = typeof(FolderItems);
            //GridView.ItemsSourceProperty
        }
        private SolidColorBrush bcolor;
        private IData _topright;
        private IData _bottomleft;
        private IData _bottomright;
        public IData topright_item { 
            set 
            {
                topright_text.Text = value.Name;
                topright_icon.Source = value.DataIcon;
                _topright = value;
            }
            get
            {
                return _topright;
            }
        }
        public IData bottomleft_item
        {
            set
            {
                bottomleft_text.Text = value.Name;
                bottomleft_icon.Source = value.DataIcon;
                _bottomleft = value;
            }
            get
            {
                return _bottomleft;
            }
        }

        public IData bottomright_item
        {
            set
            {
                bottomright_text.Text = value.Name;
                bottomright_icon.Source = value.DataIcon;
                _bottomright = value;
            }
            get
            {
                return _bottomright;
            }
        }

        private void createView(Folder f)
        {
            title.Text = f.Name;
        }
        public static readonly DependencyProperty FolderSourceProperty =
            DependencyProperty.Register("FolderSource", typeof(Folder), typeof(FolderItems), new PropertyMetadata(null,new PropertyChangedCallback(OnFolderChangedCallback)));

        public static void OnFolderChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            FolderItems folderItems = obj as FolderItems;
            Folder folder = e.NewValue as Folder;
            //Debug.WriteLine(obj);
            //Debug.WriteLine(e);
            folderItems.title.Text = folder.Name;
            folderItems.topleft_numberoffiles.Text = "" +(folder.Files.Count + folder.SubFolders.Count);
            int i = 0;
            foreach(File f in folder.Files)
            {
                switch (i)
                {
                    case 0:
                        folderItems.topright_item = f;
                        break;
                    case 1:
                        folderItems.bottomleft_item = f;
                        break;
                    case 2:
                        folderItems.bottomright_item = f;
                        break;
                    case 3: return;
                }
                i++;
            }
        }
        public Folder FolderSource 
        {
            get 
            {
 
                return (Folder)GetValue(FolderSourceProperty); 
            }
            set
            {
                createView(value);
                SetValue(FolderSourceProperty, value);
            }
        }

        //public static readonly DependencyPropertyChangedEventHandler
        private void itemclicked(object sender, ItemClickEventArgs e)
        {
            //TODO
        }

        private void topleft_tapped(object sender, TappedRoutedEventArgs e)
        {
            Debug.WriteLine("topleft");
            FolderView.Current.gotoFolder(FolderSource);
        }
        private void topright_tapped(object sender, TappedRoutedEventArgs e)
        {
            Debug.WriteLine("topright");
        }
        private void bottomleft_tapped(object sender, TappedRoutedEventArgs e)
        {
            Debug.WriteLine("bottomleft");
        }
        private void bottomright_tapped(object sender, TappedRoutedEventArgs e)
        {
            Debug.WriteLine("bottomright");
        }

        private void pointer_entered(object sender, PointerRoutedEventArgs e)
        {
            Grid obj = sender as Grid;
            bcolor = obj.Background as SolidColorBrush;
            obj.Background = new SolidColorBrush(Colors.Blue);
        }

        private void pointer_exited(object sender, PointerRoutedEventArgs e)
        {
            Debug.WriteLine("pointer exited");
            Grid obj = sender as Grid;

            obj.Background = bcolor;
        }

        private void pointer_released(object sender, PointerRoutedEventArgs e)
        {

        }

        
    }
}
